package ca.seia.comp6521.a2.itemsetstorage;


public class TriangularMatrix implements PairStorage{
	
	protected int size;
	protected int nbr;
	protected char[] counts;
	
	public TriangularMatrix(int nbr) {
		this.nbr = nbr;
//		this.size = 1999000;
		this.size = calculateSize(nbr);
		System.out.println("size of triang matr " + this.size);
		this.counts = new char[this.size];
	}
	
	public int calculateSize(int nbr) {
		return ( nbr * nbr ) /2 - nbr / 2;
	}
	
	@Override
	public void addCount(int value1, int value2) {
		this.counts[getIndexOfPair(value1, value2)]++;
	}

	@Override
	public int getPairCount(int value1, int value2) {
		int indexOfPair = getIndexOfPair(value1, value2);
		if(indexOfPair <= -1) 
			return 0;
		return this.counts[getIndexOfPair(value1, value2)];
	}
	
	public int getIndexOfPair(int value1, int value2) {
		value1++;
		value2++;
		return ((value1 - 1 ) * this.nbr - value1*(value1+1)/2 + value2) -1 ;
	}
	
	public static void main(String[] args) {
		TriangularMatrix mat = new TriangularMatrix(2000);
		System.out.println(mat.getIndexOfPair(0, 1));
		System.out.println(mat.getIndexOfPair(1998, 1999));
	}
	
}
